import type { LedgerSnapshot } from "@agent-society/types";

const metrics = [
  { key: "cpu", label: "CPU", format: (value: number) => `${value.toFixed(0)}%`, accent: "text-sky-300" },
  { key: "memory", label: "Memory", format: (value: number) => `${value.toFixed(2)} GB`, accent: "text-emerald-300" },
  { key: "logs", label: "Log Volume", format: (value: number) => `${value.toFixed(1)} MB`, accent: "text-amber-300" }
] as const;

export interface MetricsRailProps {
  ledger: LedgerSnapshot;
}

export function MetricsRail({ ledger }: MetricsRailProps) {
  return (
    <aside className="surface-panel flex h-full flex-col gap-4 p-4">
      <header>
        <h2 className="text-lg font-semibold text-slate-100">System Metrics</h2>
        <p className="text-sm text-slate-400">Updated {new Date(ledger.updatedAt).toLocaleTimeString()}</p>
      </header>
      <div className="space-y-3">
        {metrics.map(metric => (
          <div key={metric.key} className="rounded-lg border border-slate-700/40 bg-slate-900/40 p-3">
            <p className="text-xs uppercase tracking-wide text-slate-500">{metric.label}</p>
            <p className={`mt-1 text-2xl font-semibold ${metric.accent}`}>
              {metric.format(generateSample(metric.key, ledger))}
            </p>
          </div>
        ))}
      </div>
      <section className="rounded-lg border border-slate-700/40 bg-slate-900/30 p-3 text-sm text-slate-300">
        <header className="mb-2 text-xs uppercase tracking-wide text-slate-500">Credit flow summary</header>
        <ul className="space-y-1 text-xs">
          <li className="flex justify-between"><span>Issuance</span><span className="text-sky-300">+{ledger.issuance}</span></li>
          <li className="flex justify-between"><span>Charges</span><span className="text-rose-300">-{ledger.charges}</span></li>
          <li className="flex justify-between"><span>Velocity</span><span className="text-emerald-300">{ledger.velocity.toFixed(1)}/hr</span></li>
        </ul>
      </section>
    </aside>
  );
}

function generateSample(key: string, ledger: LedgerSnapshot) {
  const base = ledger.totalCredits;
  switch (key) {
    case "cpu":
      return (base % 60) + 25;
    case "memory":
      return (base % 12) / 3 + 2.1;
    case "logs":
      return (ledger.charges % 50) + 12;
    default:
      return 0;
  }
}
